<CFIF lPreProcessShowMsg EQ "Yes">
<table border=0 cellpadding="1px" cellspacing=0 width="100%">
</CFIF>


<CFQUERY NAME="qryCLKLVCODE" DATASOURCE=#client.company_dsn#>
	SELECT WITHBALANCE, ALLOWNEGBALANCE
	FROM CLKLVCODE
	WHERE LEAVETYPE = '#Evaluate("GetEntry.LEAVETYPE")#'
</CFQUERY>
<CFSET theDate= #DATEFORMAT(GETENTRY.STARTINGDATE, "mm/dd/yyyy")#>
<CFSET sNoofDays= DateDiff("d", "#GETENTRY.STARTINGDATE#", "#GETENTRY.ENDINGDATE#") + 1>
<CFSET theRefDate="">
<CFOUTPUT QUERY="GETENTRY">
	<CFLOOP index="noofdays" FROM="1" TO="#sNoofDays#" STEP="1">
		<CFSET ValidateDate=DayOfWeekAsString(DayOfWeek("#theDate#"))>
		<CFIF ValidateDate NEQ "Sunday">
			<CFIF ValidateDate NEQ "Saturday">
				<CFIF GETENTRY.STAFFPID NEQ "">
					<CFQUERY NAME="qryLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
						SELECT REFERENCEDATE
						FROM CINLEAVEAPPSD
						WHERE PERSONNELIDNO = '#GETENTRY.STAFFPID#' AND REFERENCEDATE = '#theDate#'
					</CFQUERY>
				<CFELSE>
					<CFQUERY NAME="qryLEAVEAPPSD" DATASOURCE="#client.company_dsn#">
						SELECT REFERENCEDATE
						FROM CINLEAVEAPPSD
						WHERE PERSONNELIDNO = '#GETENTRY.PERSONNELIDNO#' AND REFERENCEDATE = '#theDate#'
					</CFQUERY>
				</CFIF>
				<CFIF qryLEAVEAPPSD.RECORDCOUNT>
				   <cfbreak>
				   <CFSET isValidatedEntry="true">
			     </cfif>
			</CFIF>
		</CFIF>
		<CFSET ValidateDate="">
		<CFSET theDate= DATEFORMAT(DateAdd("D",1, "#theDate#"))>
	</CFLOOP>
</CFOUTPUT>
<CFQUERY NAME="qryLeaveType" DATASOURCE="#client.company_dsn#">
	SELECT LEAVETYPE,DESCRIPTION
	FROM CLKLVCODE
	WHERE (DESCRIPTION LIKE 'SERVICE%' OR DESCRIPTION LIKE 'SABBAT%' OR DESCRIPTION LIKE 'BUSINESS%')
</CFQUERY>
<CFQUERY NAME="qryJOBGRADE" DATASOURCE="#client.company_dsn#">
		SELECT JOBGRADECODE
		FROM CLKJOBGRADE
		WHERE DESCRIPTION LIKE '%ASF%' OR DESCRIPTION LIKE '%INSTRUCTOR%' OR DESCRIPTION LIKE '%PROF%' OR DESCRIPTION LIKE '%FACULTY%'
							OR DESCRIPTION LIKE '%LECTURE%'
</CFQUERY>
<CFIF NOT LISTCONTAINS(qryLeaveType.LEAVETYPE, GETENTRY.LEAVETYPE)>
	<CFIF NOT qryCLKLVCODE.RecordCount>
		<CFIF lPreProcessShowMsg EQ "Yes">
		<tr><td nowrap class="bold">Leave Type not found in Lookup table.</td></tr>
		</CFIF>
	<CFELSEIF isdefined("isValidatedEntry")>
		<CFIF lPreProcessShowMsg EQ "Yes">
		<tr><td nowrap class="bold"><CFOUTPUT>Leave with date #DATEFORMAT(qryLEAVEAPPSD.REFERENCEDATE,"mmmm dd,yyyy")# has already been availed.</CFOUTPUT></td></tr>
		</CFIF>
	<CFELSEIF Trim(Ucase(qryCLKLVCODE.WITHBALANCE)) IS "N">
		<CFIF lPreProcessShowMsg EQ "Yes">
		<tr><td nowrap class="bold">Leave Type has unlimited balance.</td></tr>
		</CFIF>
		<CFSET lPreProcessValidated="Yes">
	<CFELSE>
		<CFIF isDefined("GetEntry.STAFFPID")>
			<CFIF GetEntry.STAFFPID NEQ "">
				<CFSET formPID = GetEntry.STAFFPID>
			<CFELSE>
				<CFSET formPID = GetEntry.PERSONNELIDNO>
			</CFIF>
		<CFELSE>
			<CFSET formPID = GetEntry.PERSONNELIDNO>
		</CFIF>
		<CFQUERY NAME="qryJOBGRADE" DATASOURCE="#client.company_dsn#">
			SELECT JOBGRADECODE FROM CMFPA
			WHERE PERSONNELIDNO = '#formPID#' <!---MICHELLE--->
		</CFQUERY>

		<CFQUERY NAME="qryCINLVENTITLEMENT" DATASOURCE="#client.company_dsn#">
			SELECT LEAVEENTITLEMENT, TYPE, STARTDATE, ACCRUEDBALANCE, ACCRUEDBALANCEASOF,
					LEAVEBALANCE, LEAVEBALANCEASOF, AVAILEDLEAVE, AVAILEDLEAVEASOF, LEAVETYPE
			FROM CINLVENTITLEMENT
			WHERE PERSONNELIDNO = '#formPID#' AND
				LEAVETYPE = '#Evaluate("GetEntry.LEAVETYPE")#'
		</CFQUERY>

		<CFIF qryCINLVENTITLEMENT.RecordCount>
			<CFQUERY NAME="qryCBR201" DATASOURCE=#client.company_dsn# MAXROWS=1>
				SELECT LEAVETRACKUNIT
				FROM CBR201
			</CFQUERY>

			<CFIF qryCBR201.RecordCount>
				<!--- Check workingdayscode --->
				<CFQUERY NAME="qryCMFPA" DATASOURCE="#client.company_dsn#">
					SELECT *
					FROM CMFPA
					WHERE PERSONNELIDNO = '#formPID#'
				</CFQUERY>

				<CFQUERY NAME="qryCLKWORKINGDAYS" DATASOURCE="#client.company_dsn#">
					SELECT WORKINGHOURSPDAY,WORKINGHOURSFORLV
					FROM CLKWORKINGDAYS
					WHERE WORKINGDAYSCODE = '#qryCMFPA.WORKINGDAYSCODE#'
				</CFQUERY>

				<CFIF qryCLKWORKINGDAYS.RecordCount>
					<!--- Check if noofhours exceed noofdays --->
					<CFSET nHoursInDays=DateDiff("d", Evaluate("GetEntry.STARTINGDATE"), Evaluate("GetEntry.ENDINGDATE")) + 1>
	<!---				modified by soti to be in sync with updateLVBalance.cfm
					<CFSET nHoursApplied=Evaluate("GetEntry.LEAVEHOURS")>

					<CFIF IsDefined("GetEntry.NOOFDAYS") AND Trim(LCase(qryCBR201.LEAVETRACKUNIT)) EQ "by day">
						<CFSET nHoursApplied=Evaluate("GetEntry.NOOFDAYS")>
					</CFIF> --->

					<CFIF Trim(LCase(qryCBR201.LEAVETRACKUNIT)) IS "by day">
						<CFSET nHoursApplied = val(GetEntry.NOOFDAYS) + (val(GetEntry.LEAVEHOURS) / val(qryCLKWORKINGDAYS.WORKINGHOURSFORLV))>
					<CFELSE>
						<CFSET nHoursApplied = (val(GetEntry.NOOFDAYS) * val(qryCLKWORKINGDAYS.WORKINGHOURSFORLV)) + val(GetEntry.LEAVEHOURS)>
					</CFIF>

	<!---
					<CFIF nHoursInDays GTE 0>
						<CFSET nHoursInDays=nHoursInDays + 1>
					</CFIF>
	--->
					<CFIF nHoursApplied NEQ "" AND nHoursApplied NEQ 0>
						<CFIF Trim(LCase(qryCBR201.LEAVETRACKUNIT)) NEQ "by day">
							<CFSET nHoursInDays=nHoursInDays * qryCLKWORKINGDAYS.WORKINGHOURSPDAY>
						</CFIF>

						<CFIF nHoursApplied LTE nHoursInDays>
							<CFSET nLVHours=val(qryCINLVENTITLEMENT.LEAVEBALANCE)>

							<CFIF LCase(#GetEntry.WITHPAY#) EQ "Y">
								<CFSET nLVBalance=nLVHours - nHoursApplied>
							<CFELSE>
								<CFSET nLVBalance=nLVHours>
							</CFIF>

							<CFOUTPUT>
							<CFIF lPreProcessShowMsg EQ "Yes">
							<tr>
								<!---by jepoy--->
								<td width="25%">
									<table border=0 cellpadding=2 cellspacing=0 width="100%">
									<tr>
										<td nowrap>Personnel ID No.&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCMFPA.PERSONNELIDNO#</td>
									</tr>
									<tr>
										<td nowrap>Last Name&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCMFPA.LASTNAME#</td>
									</tr>
									<tr>
										<td nowrap>First Name&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCMFPA.FIRSTNAME#</td>
									</tr>
									</table>
								</td>
								<td bgcolor="##808080"></td>
								<!---by jepoy END HERE--->
								<td width="25%">
									<table border=0 cellpadding=2 cellspacing=0 width="100%">
									<tr>
										<td nowrap>Leave Type&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.LEAVETYPE#</td>
									</tr>
									<tr>
										<td nowrap>Leave Balance as of cut-off&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.LEAVEBALANCE#</td>
									</tr>
									<tr>
										<td nowrap>Leave Balance as of&nbsp;</td>
										<td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.LEAVEBALANCEASOF, "mm/dd/yyyy")#</td>
									</tr>
									</table>
								</td>
								<td bgcolor="##808080"></td>
								<td width="25%">
									<table border=0 cellpadding=2 cellspacing=0 width="100%">
									<tr>
										<td nowrap>&nbsp;Type&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.TYPE#</td>
									</tr>
									<tr>
										<td nowrap>&nbsp;Entitlement&nbsp;</td>
										<td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.LEAVEENTITLEMENT#</td>
									</tr>
									<tr>
										<td nowrap>&nbsp;Start Date&nbsp;</td>
										<td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.STARTDATE, "mm/dd/yyyy")#</td>
									</tr>
									</table>
								</td>
								<td bgcolor="##808080"></td>
								<td width="25%">
									<table border=0 cellpadding=2 cellspacing=0 width="100%">
									<CFIF UCase(qryCINLVENTITLEMENT.TYPE) IS "EARNED">
										<tr>
											<td nowrap>&nbsp;Availed Leave&nbsp;</td>
											<td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.AVAILEDLEAVE#</td>
										</tr>
										<tr>
											<td nowrap>&nbsp;Availed Leave as of&nbsp;</td>
											<td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.AVAILEDLEAVEASOF, "mm/dd/yyyy")#</td>
										</tr>
									<CFELSE>
										<tr>
											<td nowrap>&nbsp;Accrued Balance&nbsp;</td>
											<td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.ACCRUEDBALANCE#</td>
										</tr>
										<tr>
											<td nowrap>&nbsp;Accrued Balance as of&nbsp;</td>
											<td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.ACCRUEDBALANCEASOF, "mm/dd/yyyy")#</td>
										</tr>
									</CFIF>
									<CFIF UCase(Evaluate("GetEntry.APPROVED")) NEQ "Y" OR UCase(Evaluate("GetEntry.APPROVED")) NEQ "C">
										<tr>
											<td nowrap>&nbsp;Leave Balance if Approved&nbsp;</td>
											<td nowrap width="100%" class="bold">
												#nLVBalance#
												<CFIF nLVBalance LT 0 AND qryCLKLVCODE.ALLOWNEGBALANCE NEQ "Y">
													(Negative Balance is not allowed)
												</CFIF>
												<CFIF LCase(#GetEntry.WITHPAY#) NEQ "Y">
													(Leave Application is without pay)
												</CFIF>
											</td>
										</tr>
									</CFIF>
									</table>
								</td>
							</tr>
							</CFIF>
							<!--- Check if employee has enough leave balance --->
							<CFIF (nLVBalance GE 0) OR
								  (qryCLKLVCODE.ALLOWNEGBALANCE IS "Y")>
								<CFSET lPreProcessValidated="Yes">
							</CFIF>
							</CFOUTPUT>
						<CFELSE>
							<CFIF lPreProcessShowMsg EQ "Yes">
							<tr><td nowrap class="bold">No. of days/hours applied exceeds No. of days.</td></tr>
							</CFIF>
						</CFIF>
					<CFELSE>
						<CFIF lPreProcessShowMsg EQ "Yes">
						<tr><td nowrap class="bold">No. of days/hours applied is 0.</td></tr>
						</CFIF>
					</CFIF>
				<CFELSE>
					<CFIF lPreProcessShowMsg EQ "Yes">
					<tr><td nowrap class="bold">Invalid WORKINGDAYSCODE.</td></tr>
					</CFIF>
				</CFIF>
			<CFELSE>
				<CFIF lPreProcessShowMsg EQ "Yes">
				<tr><td nowrap class="bold">Leave Track Unit is not properly configured.</td></tr>
				</CFIF>
			</CFIF>
		<CFELSE>
			<CFIF lPreProcessShowMsg EQ "Yes">
			<tr><td nowrap class="bold">Leave Type not found in CINLVENTITLEMENT table.</td></tr>
			</CFIF>
		</CFIF>
	</CFIF>
<CFELSE>
	<CFIF NOT qryCLKLVCODE.RecordCount>
		<CFIF lPreProcessShowMsg EQ "Yes">
		<tr><td nowrap class="bold">Leave Type not found in Lookup table.</td></tr>
		</CFIF>
	<CFELSEIF qryLEAVEAPPSD.RECORDCOUNT>
		<CFIF lPreProcessShowMsg EQ "Yes">
		<tr><td nowrap class="bold"><CFOUTPUT>Leave with date #DATEFORMAT(qryLEAVEAPPSD.REFERENCEDATE,"mmmm dd,yyyy")# has already been availed.</CFOUTPUT></td></tr>
		</CFIF>
	<CFELSEIF Trim(Ucase(qryCLKLVCODE.WITHBALANCE)) IS "N">
		<CFIF lPreProcessShowMsg EQ "Yes">
		<tr><td nowrap class="bold">Leave Type has unlimited balance.</td></tr>
		</CFIF>
		<CFSET lPreProcessValidated="Yes">
	<CFELSE>
		<CFIF isDefined("GetEntry.STAFFPID")>
			<CFIF GetEntry.STAFFPID NEQ "">
				<CFSET formPID = GetEntry.STAFFPID>
			<CFELSE>
				<CFSET formPID = GetEntry.PERSONNELIDNO>
			</CFIF>
		<CFELSE>
			<CFSET formPID = GetEntry.PERSONNELIDNO>
		</CFIF>
		<CFQUERY NAME="qryJOBGRADE" DATASOURCE="#client.company_dsn#">
			SELECT JOBGRADECODE FROM CMFPA
			WHERE PERSONNELIDNO = '#formPID#'
		</CFQUERY>
		<CFQUERY NAME="qryCMFPA" DATASOURCE="#client.company_dsn#">
			SELECT *
			FROM CMFPA
			WHERE PERSONNELIDNO = '#formPID#'
		</CFQUERY>

		<CFQUERY NAME="qryCINLVENTITLEMENT" DATASOURCE="#client.company_dsn#">
			SELECT LEAVEENTITLEMENT, TYPE, STARTDATE, ACCRUEDBALANCE, ACCRUEDBALANCEASOF,
					LEAVEBALANCE, LEAVEBALANCEASOF, AVAILEDLEAVE, AVAILEDLEAVEASOF, LEAVETYPE
			FROM CINLVENTITLEMENT
			WHERE PERSONNELIDNO = '#formPID#' AND
				LEAVETYPE = '#Evaluate("GetEntry.LEAVETYPE")#'
		</CFQUERY>
		<CFIF LCase(#GetEntry.WITHPAY#) EQ "Y">
			 <CFSET nLVBalance= qryCINLVENTITLEMENT.LEAVEBALANCE-GETENTRY.NOOFDAYS>
		 <CFELSE>
			 <CFSET nLVBalance=qryCINLVENTITLEMENT.LEAVEBALANCE>
		 </CFIF>

		<CFIF qryCINLVENTITLEMENT.RecordCount>
			<CFIF GETENTRY.NOOFDAYS LTE qryCINLVENTITLEMENT.LEAVEBALANCE>
				   <CFOUTPUT>
				   <CFIF lPreProcessShowMsg EQ "Yes">
				   <CFSET lPreProcessValidated="Yes">
				   <tr>
					   <!---by jepoy--->
					   <td width="25%">
						   <table border=0 cellpadding=2 cellspacing=0 width="100%">
						   <tr>
							   <td nowrap>Personnel ID No.&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCMFPA.PERSONNELIDNO#</td>
						   </tr>
						   <tr>
							   <td nowrap>Last Name&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCMFPA.LASTNAME#</td>
						   </tr>
						   <tr>
							   <td nowrap>First Name&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCMFPA.FIRSTNAME#</td>
						   </tr>
						   </table>
					   </td>
					   <td bgcolor="##808080"></td>
					   <!---by jepoy END HERE--->
					   <td width="25%">
						   <table border=0 cellpadding=2 cellspacing=0 width="100%">
						   <tr>
							   <td nowrap>Leave Type&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.LEAVETYPE#</td>
						   </tr>
						   <tr>
							   <td nowrap>Leave Balance as of cut-off&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.LEAVEBALANCE#</td>
						   </tr>
						   <tr>
							   <td nowrap>Leave Balance as of&nbsp;</td>
							   <td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.LEAVEBALANCEASOF, "mm/dd/yyyy")#</td>
						   </tr>
						   </table>
					   </td>
					   <td bgcolor="##808080"></td>
					   <td width="25%">
						   <table border=0 cellpadding=2 cellspacing=0 width="100%">
						   <tr>
							   <td nowrap>&nbsp;Type&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.TYPE#</td>
						   </tr>
						   <tr>
							   <td nowrap>&nbsp;Entitlement&nbsp;</td>
							   <td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.LEAVEENTITLEMENT#</td>
						   </tr>
						   <tr>
							   <td nowrap>&nbsp;Start Date&nbsp;</td>
							   <td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.STARTDATE, "mm/dd/yyyy")#</td>
						   </tr>
						   </table>
					   </td>
					   <td bgcolor="##808080"></td>
					   <td width="25%">
						   <table border=0 cellpadding=2 cellspacing=0 width="100%">
						   <CFIF UCase(qryCINLVENTITLEMENT.TYPE) IS "EARNED">
							   <tr>
								   <td nowrap>&nbsp;Availed Leave&nbsp;</td>
								   <td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.AVAILEDLEAVE#</td>
							   </tr>
							   <tr>
								   <td nowrap>&nbsp;Availed Leave as of&nbsp;</td>
								   <td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.AVAILEDLEAVEASOF, "mm/dd/yyyy")#</td>
							   </tr>
						   <CFELSE>
							   <tr>
								   <td nowrap>&nbsp;Accrued Balance&nbsp;</td>
								   <td nowrap width="100%" class="bold">#qryCINLVENTITLEMENT.ACCRUEDBALANCE#</td>
							   </tr>
							   <tr>
								   <td nowrap>&nbsp;Accrued Balance as of&nbsp;</td>
								   <td nowrap width="100%" class="bold">#DateFormat(qryCINLVENTITLEMENT.ACCRUEDBALANCEASOF, "mm/dd/yyyy")#</td>
							   </tr>
						   </CFIF>
						   <CFIF UCase(Evaluate("GetEntry.APPROVED")) NEQ "Y" OR UCase(Evaluate("GetEntry.APPROVED")) NEQ "C">
							   <tr>
								   <td nowrap>&nbsp;Leave Balance if Approved&nbsp;</td>
								  <td nowrap width="100%" class="bold">
										  #nLVBalance#
										  <CFIF nLVBalance LT 0 AND qryCLKLVCODE.ALLOWNEGBALANCE NEQ "Y">
											  (Negative Balance is not allowed)
										  </CFIF>
										  <CFIF LCase(#GetEntry.WITHPAY#) NEQ "Y">
											  (Leave Application is without pay)
										  </CFIF>
									</td>
							   </tr>
						   </CFIF>
						   </table>
					   </td>
				   </tr>
				   </CFIF>
				   </CFOUTPUT>
			<CFELSE>
					<CFIF lPreProcessShowMsg EQ "Yes">
						<tr><td nowrap class="bold">No. of leave applied exceeds No. of Entitled Leave.</td></tr>
				     </CFIF>
			</CFIF>
		<CFELSE>
			<CFIF lPreProcessShowMsg EQ "Yes">
			<tr><td nowrap class="bold">Leave Type not found in CINLVENTITLEMENT table.</td></tr>
			</CFIF>
			
		</CFIF>
	</CFIF>
</CFIF>
<CFIF lPreProcessShowMsg EQ "Yes">
</table>
</CFIF>
